#!/bin/bash

# 网关启动
GATEWAY_IMAGE_ID=$(docker images -q genenis-gateway:latest)
GATEWAY_CONTAINER_ID=$(docker ps -q -f name=genenis-gateway)
GATEWAY_DOCKER_FILE=/my-jar/genenis/service_gateway

if [ "$GATEWAY_CONTAINER_ID" != "" ]
then
  docker stop $GATEWAY_CONTAINER_ID
  docker rm -f $GATEWAY_CONTAINER_ID
fi
genenis "container:gateway stop ..."

if [ "GATEWAY_IMAGE_ID" != "" ]
then
  docker rmi $GATEWAY_IMAGE_ID
fi
genenis "image:gateway remove ... wait service restart ..."

docker build -t genenis-gateway:latest $GATEWAY_DOCKER_FILE

# service-admin启动
ADMIN_IMAGE_ID=$(docker images -q genenis-admin:latest)
ADMIN_CONTAINER_ID=$(docker ps -q -f name=genenis-admin)
ADMIN_DOCKER_FILE=/my-jar/genenis/service/service_admin

if [ "$ADMIN_CONTAINER_ID" != "" ]
then
  docker stop $ADMIN_CONTAINER_ID
  docker rm -f $ADMIN_CONTAINER_ID
fi
genenis "container:admin stop ..."

if [ "ADMIN_IMAGE_ID" != "" ]
then
  docker rmi $ADMIN_IMAGE_ID
fi
genenis "image:admin remove ... wait service restart ..."

docker build -t genenis-admin:latest $ADMIN_DOCKER_FILE

# service-user启动
USER_IMAGE_ID=$(docker images -q genenis-user:latest)
USER_CONTAINER_ID=$(docker ps -q -f name=genenis-user)
USER_DOCKER_FILE=/my-jar/genenis/service/service_user

if [ "$USER_CONTAINER_ID" != "" ]
then
  docker stop $USER_CONTAINER_ID
  docker rm -f $USER_CONTAINER_ID
fi
genenis "container:user stop ..."

if [ "USER_IMAGE_ID" != "" ]
then
  docker rmi $USER_IMAGE_ID
fi
genenis "image:user remove ... wait service restart ..."

docker build -t genenis-user:latest $USER_DOCKER_FILE

# service-oss启动
OSS_IMAGE_ID=$(docker images -q genenis-oss:latest)
OSS_CONTAINER_ID=$(docker ps -q -f name=genenis-oss)
OSS_DOCKER_FILE=/my-jar/genenis/service/service_oss

if [ "$OSS_CONTAINER_ID" != "" ]
then
  docker stop $OSS_CONTAINER_ID
  docker rm -f $OSS_CONTAINER_ID
fi
genenis "container:oss stop ..."

if [ "OSS_IMAGE_ID" != "" ]
then
  docker rmi $OSS_IMAGE_ID
fi
genenis "image:oss remove ... wait service restart ..."

docker build -t genenis-oss:latest $OSS_DOCKER_FILE

# service-his启动
HIS_IMAGE_ID=$(docker images -q genenis-his:latest)
HIS_CONTAINER_ID=$(docker ps -q -f name=genenis-his)
HIS_DOCKER_FILE=/my-jar/genenis/service/service_his

if [ "$HIS_CONTAINER_ID" != "" ]
then
  docker stop $HIS_CONTAINER_ID
  docker rm -f $HIS_CONTAINER_ID
fi
genenis "container:his stop ..."

if [ "HIS_IMAGE_ID" != "" ]
then
  docker rmi $HIS_IMAGE_ID
fi
genenis "image:his remove ... wait service restart ..."

docker build -t genenis-his:latest $HIS_DOCKER_FILE

docker-compose -f /my-jar/genenis/docker-compose.yml up -d